package com.example.spritesheetanimation.actor;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

import com.example.spritesheetanimation.view.GameView;

public class Fog {

	private static final int BMP_ROWS = 1;

	private static final int BMP_COLUMNS = 2;

	private static final int MAX_SPEED = 5;

	private GameView gameView;

	private Bitmap bmp;

	private int x = 0;

	private int y = 0;

	private int xSpeed = 5;

	private int ySpeed = 5;

	private int currentFrame = 0;

	private int width;

	private int height;

	public Fog(GameView gameView, Bitmap bmp) {

		this.width = bmp.getWidth() / BMP_COLUMNS;

		this.height = bmp.getHeight() / BMP_ROWS;

		this.gameView = gameView;

		this.bmp = bmp;
		
		lastClick = System.currentTimeMillis();

		// Random rnd = new Random();
		//
		// x = rnd.nextInt(gameView.getWidth() - width);
		//
		// y = rnd.nextInt(gameView.getHeight() - height);

		// xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
		//
		// ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;

	}

	private void update() {

		if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {

			xSpeed = -xSpeed;

		}

		x = x + xSpeed;

		if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {

			ySpeed = -ySpeed;

		}

		y = y + ySpeed;
		
		Log.d("Kai", ( (System.currentTimeMillis() - lastClick)) +" ");

		if ( System.currentTimeMillis() - lastClick > 300) {

			lastClick = System.currentTimeMillis();
			currentFrame = ++currentFrame % BMP_COLUMNS;
		}
	}

	long lastClick;

	public void onDraw(Canvas canvas) {

		update();

		int srcX = currentFrame * width;

		int srcY = 0 * height;

		Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);

		Rect dst = new Rect(x, y, x + width, y + height);

		canvas.drawBitmap(bmp, src, dst, null);

	}

	public boolean isCollition(float x2, float y2) {

		return x2 > x && x2 < x + width && y2 > y && y2 < y + height;

	}
}
